<?php
declare (strict_types = 1);

namespace app\admin\controller\wx;
use EasyWeChat\Kernel\Messages\Article;
class Material extends WxBase
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new \app\common\model\Material;
        $this->validate = new \app\common\validate\Material;
    }
    // 设置搜索条件
    protected function searchConditionHandle(&$where)
    {
        $search = $this->request->param('search',null);
        if($search) {
            $search = json_decode($search, true);
            if(is_array($search) && sizeof($search) > 0) {
                ((array_key_exists('type', $search) && !empty($search['type'])) && $where[] = ['type','=', $search['type']]);
                ((array_key_exists('title', $search) && !empty($search['title'])) && $where[] = ['title|digest','like', '%'.$search['title'].'%']);
                // ((array_key_exists('status', $search) && !empty($search['status'])) && $where[] = ['status','=', $search['status']]);
                // ((array_key_exists('category', $search) && !empty($search['category'])) && $where[] = ['category', '=', $search['category'][0]]);
            }
        }
    }
    public function upload() {
        $data = $this->request->post();
        $path = '.'.$this->public_path.'/'.$data['filepath'];
        switch ($data['type']) {
            case 'image':
                $result = $this->app->material->uploadImage($path);
                break;
            case 'voice':
                $result = $this->app->material->uploadVoice($path);
                break;
            case 'video':
                $result = $this->app->material->uploadVideo($path);
                break;
            case 'thumb':
                $result = $this->app->material->uploadThumb($path);
                break;
            case 'artical':
                $article = new Article([
                    'title'=>$data['title'],
                    'thumb_media_id'=>'CLzq_HW_UmlNCEwzgqUIw9cy8Dp2EUul3f_qzhzgYjs',
                    'author'=>$data['author'],
                    'digest'=>$data['digest'],
                    'content'=>$data['content'],
                    'show_cover'=>$data['show_cover_pic'],
                    'content_source_url'=>intval($data['content_source_url']),
                    'need_open_comment'=>intval($data['need_open_comment']),
                    'only_fans_can_comment'=>intval($data['only_fans_can_comment'])
                ]);
                $result = $this->app->material->uploadArticle($article);
                break;
            default:
                # code...
                return json('错误的type',500);
                break;
        }
        $material = array_merge($data,$result);
        $material['author_id'] = $this->admin_id;
        if(!$this->validate->scene('create')->check($material)){
            return json($this->validate->getError(),500);
        }else {
            $this->model->save($material);
            return json('新增成功！');
        }
    }
    public function upload_article() {
        // 上传单篇图文
        $article = new Article([
            'title' => 'xxx',
            'thumb_media_id' => $mediaId,
            //...
        ]);
        $this->app->material->uploadArticle($article);

        // 或者多篇图文
        $this->app->material->uploadArticle([$article, $article2]);
    }
    public function update_article() {
        $result = $this->app->material->updateArticle($mediaId, new Article());
        // or
        $result = $app->material->updateArticle($mediaId, [
        'title' => 'EasyWeChat 4.0 发布了！',
            'thumb_media_id' => 'qQFxUQGO21Li4YrSn3MhnrqtRp9Zi3cbM9uBsepvDmE', // 封面图片 mediaId
            'author' => 'overtrue', // 作者
            'show_cover' => 1, // 是否在文章内容显示封面图片
            'digest' => '这里是文章摘要',
            'content' => '这里是文章内容，你可以放很长的内容',
            'source_url' => 'https://www.easywechat.com',
        ]);
    }
    public function update_article_image() {
        $result = $this->app->material->uploadArticleImage($path);
    }
    public function stats(){
        $stats = $this->app->material->stats();
        return json($stats);
    }
    public function delete_material(){
        $mediaId = $this->request->param('media_id', null);
        if(empty($mediaId)){
            return null;
        }
        $res = $this->app->material->delete($mediaId);
        return json($res);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if ($id === null) return json('参数错误！',500);
        $material = $this->model->where('id', $id)->find();
        if($material === null) {
            return json('未找到数据',500);
        }
        $res = $this->app->material->delete($material['media_id']);
        $this->model->destroy($id);
        return json('删除成功！');
    }
}
